#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use core_api::{app_handle, app_log};
fn main() {
    // cargo run -- list_devices
    let app_name = "ff7scre";
    app_log::config_log(app_name);

    let args = ff7scre_lib::get_args();

    match app_handle::get_app_handle(app_name) {
        Err(err) => {
            log::info!("{}", err);
            std::process::exit(-1);
        }

        Ok(handle) => {
            if args.len() > 1 {
                ff7scre_lib::init_gmm();
                let cmd = &args[0];
                match cmd.as_str() {
                    "start_recording" => {
                        // cargo run -- start_recording d:\\output1.mkv  薛1  false 10
                        // Command line arguments: ["\\\\?\\D:\\...\\ff7scre.exe", "start_recording", "d:\\output1.mkv", "薛1", "false"]
                        if let [
                            _cmd,
                            output_file,
                            watermark_text,
                            force_open_camera,
                            t,
                            bwexam_pid,
                            ..,
                        ] = &args[..]
                        {
                            // p1: d:\output1.mkv, p2: 薛1, p3: false
                            log::info!(
                                "output_file: {}, watermark_text: {}, force_open_camera: {},t={}, bwexam_pid: {},my_pid={}",
                                output_file,
                                watermark_text,
                                force_open_camera,
                                t,          // 用于测试运行多长时间
                                bwexam_pid,  //主进程 PID
                                std::process::id() //自己的id
                            );

                            // ctrlc::set_handler(move || {
                            //     log::info!("recive ctr+c...");

                            //     std::process::exit(0);
                            // })
                            // .expect("set Ctrl+C handler error");

                            // std::panic::set_hook(Box::new(|panic_info| {
                            //     log::error!("panic: {:?}", panic_info);
                            // }));

                            let _ = ff7scre_lib::start_recording(
                                std::path::PathBuf::from(output_file).as_path(),
                                watermark_text.clone(),
                                force_open_camera.parse::<bool>().unwrap(),
                                t.parse::<u64>().unwrap(),
                                bwexam_pid.parse::<u32>().unwrap(),
                            );
                        }
                    }
                    _ => (),
                }
            }

            app_handle::close_app_handle(handle);
            log::info!("{} exit", app_name);
        }
    }
}
